/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package asja.formulario;

import asja.dao.LoginDao;
import asja.modelo.LoginModelo;
import asja.outras.Conversoes;
import java.awt.*;
import java.awt.event.*;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;

/**
 *
 * @author Alice
 */
public class LoginFormulario extends JFrame {
    
    private JLabel lbl_usuario, lbl_senha;  
    private JTextField jtf_usuario;  
    private JPasswordField pass;    
    private JButton btOK, btCancelar;
    List <LoginModelo> usuarios;
    
    
     public LoginFormulario() 
    {        
        
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        this.setTitle("Login");  
        this.setSize(400, 200);          
        this.setLocationRelativeTo(null);  
    } 
     
     
    
     
     // criando a janela
    public void criaJanela()  
    {     
        
        //criando os objetos
        lbl_usuario = new JLabel(" Usuário "); 
        jtf_usuario = new JTextField("", 20);  
        
        
        lbl_senha = new JLabel("  Senha  ");  
        pass=new JPasswordField("", 20); 
        
        
        
        btOK = new JButton("OK");  
        btOK.setPreferredSize(new Dimension(100,30));        
        
        btCancelar = new JButton("Cancelar");
        btCancelar.setPreferredSize(new Dimension(100,30));
        
       
        
        
        //painel dos botões
        JPanel painel_comandos = new JPanel();  
        painel_comandos.setLayout(new FlowLayout());
        painel_comandos.add(btOK);
        painel_comandos.add(btCancelar);
        
        //painel para rotulo usuario
        JPanel painel_usuario = new JPanel();  
        painel_usuario.setLayout(new FlowLayout());
        painel_usuario.add(lbl_usuario);
        painel_usuario.add(jtf_usuario);
        
        //painel para rotulo senha
        JPanel painel_senha = new JPanel();  
        painel_senha.setLayout(new FlowLayout());
        painel_senha.add(lbl_senha);
        painel_senha.add(pass);
        
        
        //painel no norte
        JPanel painel_norte = new JPanel();  
        painel_norte.setLayout(new BorderLayout());
        painel_norte.add(painel_usuario, BorderLayout.NORTH);
        painel_norte.add(painel_senha, BorderLayout.SOUTH);
        painel_norte.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(), "Login"));
           
        
        
        //criando o painel principal e adicionando ao JFrame
        JPanel painel_geral = new JPanel();
        this.getContentPane().add(painel_geral);    
        painel_geral.setLayout(new BorderLayout());       
        
        
        //adicionando o painel de comandos ao painel principal, usando o borderLayout.south        
        painel_geral.add(painel_comandos, BorderLayout.SOUTH);
        painel_geral.add(painel_norte, BorderLayout.NORTH);
        
         
        
        //Classe interna para o evento do Botão Cancelar
         btCancelar.addMouseListener(new MouseAdapter() {
        @Override
            public void mouseClicked(MouseEvent evt) {
              //mostra_login();
            MenuPrincipalFormulario mnu_principal = new MenuPrincipalFormulario(); 
            mnu_principal.criajanela_menuprincipal();  
            mnu_principal.aparencia_MenuPrincipal();
            dispose();    
       }});
         
         
         btOK.addMouseListener(new MouseAdapter() {
        @Override
            public void mouseClicked(MouseEvent evt) {
                try {
                    // cadastro_login();
                     compara_login();
                 
                 
                     //JOptionPane.showMessageDialog(null, "Certo");
                } catch (SQLException ex) {
                    Logger.getLogger(LoginFormulario.class.getName()).log(Level.SEVERE, null, ex);
                }
            }});

         
        this.setResizable(false);
        pack();        
        this.setVisible(true);
         
   
    }   
    
    /*
     public void cadastro_login(){   
 
         try {
             LoginModelo in = new LoginModelo();
             in.setUsuario(jtf_usuario.getText());
             in.setSenha(pass.getPassword().toString());
             LoginDao contr = new LoginDao();
             contr.adiciona(in);
             
          } catch (SQLException ex) {
             System.out.println("Erro!!");

         }
    
    }*/
     
    public void mostra_login(){     
  
         try{
                 LoginDao in= new LoginDao();
                 in.visualizaTodos(); 
             }catch(SQLException ex ){
                    System.out.println("Erro!!");
             }
     
     
    }
    
     public void compara_login()throws SQLException{ 
       
         LoginDao dao= new LoginDao();
         Conversoes conv= new Conversoes();
         usuarios = dao.pesquisa_login(jtf_usuario.getText(), conv.CharParaString(pass.getPassword()));
   
         
         if(usuarios.isEmpty())
           JOptionPane.showMessageDialog(this, "Usuário ou Senha Inválida");
       else
         {
           MenuPrincipalFormulario mnu_principal = new MenuPrincipalFormulario(); 
           mnu_principal.criajanela_menuprincipal();  
           mnu_principal.aparencia_MenuPrincipal();
           this.dispose();        
         
         }             
                
    }
    
    // método para aparencia
    public void aparencia(){
        
        //LOOk and feel do java começa aquiiii
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(LoginFormulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(LoginFormulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(LoginFormulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(LoginFormulario.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
    
    
    }


    
   
}
